'use strict';

const { Sequelize,Op } = require('sequelize');

const { findFiles } = require('../utils/tools')
const {host,database,uid,pwd,dialect}=require('../config')


let db = new Sequelize(database, uid, pwd, {
    host: host,
    dialect: dialect
});


let obj = {};

// 找到所有模型文件
let files = findFiles('./model');

// 定义模型

files.forEach(file => {
    let tmpObj = require('../model/' + file);
    let originName = file.replace('.js', '');
    let lowName = originName.toLowerCase();
    obj[originName] = db.define(lowName, tmpObj, {
        tableName: 'app_' + lowName,
        charset:'utf8'
    })
})

obj.Op=Op;


obj.sync = async (force) => {
    if (force) {
        return db.sync({ force: true });
    }
    return db.sync();
}

module.exports = obj;